package com.ncui.wisdomLearn.common.common;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一返回结果
 *
 * @author ncui
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
    // 预定的状态码
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_ERROR = 500;

    private Integer code;
    private String message;
    private T data;

    // 成功返回
    public static <T> R<T> ok() {
        return new R<>(CODE_SUCCESS, "ok", null);
    }

    public static <T> R<T> ok(String message) {
        return new R<>(CODE_SUCCESS, message, null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(CODE_SUCCESS, "ok", data);
    }

    public static <T> R<T> ok(String message, T data) {
        return new R<>(CODE_SUCCESS, message, data);
    }

    // 错误返回
    public static <T> R<T> error(Integer code, String message) {
        return new R<>(code, message, null);
    }

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    public boolean isOk() {
        return this.code == CODE_SUCCESS;
    }
}
